<!-- 登陆中转页面, 用于获取session, 用户信息, 权限信息等, 失败时跳转回Gate页面重新登陆 -->
<template>
    <div></div>
</template>
<script>
import { getSessionData } from "common/src/api/session";
import utils from "common/src/utils/utils";
export default {
    name: "Template",
    data() {
        return {};
    },
    created() {
        let loading = this.$loading({ fullscreen: true });
        let _sessionData = {
            uid: this.$route.query.uid,
            token: this.$route.query.token,
            ts:this.$route.query.ts
        };
        if (_sessionData.uid && _sessionData.token)
            this.init(_sessionData, loading);
        else {
            // sessionStorage.setItem("isLogin", "isLogin");
            // this.$router.replace("/login");
            this.$store.commit("clear_login_info");
            window.location.href = this.$store.state.ssourl
        }
    },
    methods: {
        init(_sessionData, loading) {
            let isAccountFreeze = false;
            getSessionData(_sessionData)
                .then(response => {
                    if (response.code == 200) {
                        sessionStorage.setItem("isLogin", "isLogin");
                        sessionStorage.setItem('sessionid',response.data&&response.data.sessionid)
                        this.$store.commit("set_session_data", response.data);

                    }else if (response.code == 313) {
                        isAccountFreeze = true;
                        this.$router.replace('/accountFreeze')
                    }  else {
                        this.$router.replace("/login");
                    }
                    loading.close();
                })
                .then(_ => {
                    if (isAccountFreeze) return
                    let targetUrl = utils.getCookie("target_url");
                    let user = JSON.parse(localStorage.getItem("sessiondata"));
                    if (user && user.isFirstLogin == "1") {
                        this.$router.replace("/welcome");
                    } else if (this.authenticate(targetUrl)) {
                        utils.setCookie("target_url", "", -1);
                        this.$router.replace(targetUrl);
                    } else{
                        this.$router.replace("/workbench");
                    } 
                });
        },
        authenticate(targetUrl) {
            if (!targetUrl) return false;
            let auths = this.$store.state.session.auths;
            return auths.some((item, index, arr) => {
                return item.urlPath == null
                    ? false
                    : item.urlPath.toLowerCase() == targetUrl.toLowerCase();
            });
        }
    }
};
</script>

